<template>
  <div class="bg-neutral-light basic-collapse" @mouseenter="isHover = true" @mouseleave="isHover = false" :style="{ background: modelValue?.bgColor || 'transparent' }">
    <div :class="[modelValue?.width ? `container-content-${modelValue?.width}` : 'container-content']">
      <div v-for="(detail, i) in modelValue?.data" :key="detail.id">
        <TheCollapse
          :index="i + 1"
          :maxIndex="modelValue?.data.length"
          :isDescription="modelValue?.isDescription"
          :collapseID="detail.id"
          :title="detail.title"
          :detail="detail.detail"
        />
      </div>
      <!-- <div v-for="detail in modelValue?.data" :key="detail.id + '-title'">
        <p :class="{ 'tx-h4 text-primary mt-4': modelValue?.isDescription }">
          {{ detail.title }}
        </p>
      </div> -->
    </div>

    <Operate v-if="!isPreview" v-model:show="isHover" @handle-edit="showSetting = true" @handle-delete="handleDelete" @handle-copy="handleCopy" @handle-paste="handlePaste"></Operate>
  </div>

  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue"></Setting>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { randomString } from '@/utils/common'
import TheCollapse from '@/common/TheCollapse/index.vue'
import Operate from '@/common/Operate/index.vue'
import Setting from './setting.vue'

// 定义组件名称
defineOptions({
  name: 'BasicCollapse'
})

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => ({})
  },
  isPreview: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['update:modelValue', 'handleDelete', 'handleCopy', 'handlePaste'])

const handleInit = () => {
  if (props.modelValue && props.modelValue.data) return // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: 'BasicCollapse',
    bgColor: '',
    width: '',
    isDescription: true,
    data: [
      {
        id: randomString(),
        title: '标题',
        detail: '<p>放假啊<strong>冷风机阿拉法久</strong>啊拉法基拉<em>法基啊激发对哦叫</em>哦金佛<s>金佛电极法金佛</s>啊</p>'
      },
      {
        id: randomString(),
        title: '标题',
        detail: '<p>放假啊冷风机阿拉法久啊拉法基阿拉法较大合法i和光i和GIA和u和韩剧v胡很尬不服啊忽然付i啊</p>'
      }
    ],
  }
  emit('update:modelValue', data)
} 

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit('handleDelete', props.modelValue.id)
}

const handleCopy = () => {
  emit('handleCopy', props.modelValue)
}

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

handleInit()
</script>

<style lang="scss" scoped>
.basic-collapse {
  position: relative;
}
.basic-collapse-title {
  font-size: 20px;
  font-weight: 600;
  line-height: 1.4;
  color: var(--text-color--primary, #0032a0);
  margin-top: 16px;
}
.bg-neutral-light {
  --bs-bg-opacity: 1;
  background-color: rgba(247,247,250,var(--bs-bg-opacity))!important;
}
</style>